﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

    public class EventLogX : EventLog
    {
        //constants
        public const string RL_LOG_NAME = "RockDotNetLib";
        public const string RL_LOG_SOURCE = "RockDotNetLib";
        public const string RL_LOG_CORE_APP_NAME = "RL CORE";

        public void AddEntry(int eventUniqueId, string logName, string source, string msg, EventLogEntryType type)
        {
            if (msg.Length >= 32765)
            {
                msg = msg.Substring(0, 32763);
            }
            // Create the source, if it does not already exist.
            if (!EventLog.SourceExists(source))
            {
                // An event log source should not be created and immediately used.
                // There is a latency time to enable the source, it should be created
                // prior to executing the application that uses the source.
                // Execute this sample a second time to use the new source.
                EventLog.CreateEventSource(source, logName);
                //Console.WriteLine("Creating EventSource");
                //Console.WriteLine("Exiting, execute the application a second time to use the source.");
                // The source is created.  Exit the application to allow it to be registered.
                return;
            }

            int myApplicationEventId = eventUniqueId;
            // Write the entry in the event log.
            //Console.WriteLine("Writing to EventLog.. ");
            EventLog.WriteEntry(source, msg, type, myApplicationEventId);
        }
        public void AddRlEntry(int eventUniqueId, string message, EventLogEntryType type)
        {
            this.AddEntry(eventUniqueId, RL_LOG_NAME, RL_LOG_SOURCE, message, type);
        }
        public void AddRLEntry(int eventUniqueId, string friendlyApplicationName, string message, EventLogEntryType type)
        {
            if (friendlyApplicationName.IsNotNullOrEmpty())
            {
                message = String.Format("Application_{0}___Message_{1}",friendlyApplicationName, message);
            }
            this.AddEntry(eventUniqueId,RL_LOG_NAME, RL_LOG_SOURCE, message, type);
        }
        public void AddRLEntry(int eventUniqueId, string message, EventLogEntryType type)
        {
            AddRLEntry(eventUniqueId,RL_LOG_CORE_APP_NAME, message, type);
        }
        public void AddRLInfoEntry(int eventUniqueId, string friendlyApplicationName, string message)
        {
            this.AddRLEntry(eventUniqueId,friendlyApplicationName,  message, EventLogEntryType.Information);
        }
        public void AddRLInfoEntry(int eventUniqueId, string message)
        {
            AddRLInfoEntry(eventUniqueId,RL_LOG_CORE_APP_NAME, message);
        }
        public void AddRLErrorEntry(int eventUniqueId, string friendlyApplicationName, string message)
        {
            this.AddRLEntry(eventUniqueId,friendlyApplicationName, message, EventLogEntryType.Error);
        }
        public void AddRLErrorEntry(int eventUniqueId, string message)
        {
            AddRLErrorEntry(eventUniqueId,RL_LOG_CORE_APP_NAME, message);
        }
        public void AddRLErrorEntry(int eventUniqueId, Exception error)
        {
            AddRLErrorEntry(eventUniqueId, error.Message + "_________" + error.StackTrace               );
        }
    }

